package com.doone.telmarket.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.doone.telmarket.domain.SiteResource;
import com.doone.telmarket.domain.factory.SiteResourceFactory;
import com.opensymphony.xwork2.ActionContext;

/**
 * @package com.doone.telmarket.domain.service
 * @author 甘雨波
 * @description 资源管理实现类
 * @date 2009-9-18
 */
@Service("siteResourceManager")
public class SiteResourceManagerImpl implements SiteResourceManager {

	Logger log = Logger.getLogger(SiteResourceManagerImpl.class);

	private String charsetName = "UTF-8";

	@Autowired
	private SiteResourceFactory siteResourceFactory;

	public String getCharsetName() {
		return charsetName;
	}

	/**
	 * @author 甘雨波
	 * @throws IOException
	 * @description 上传文件
	 * @date 2009-9-18
	 */
	public void uploadFiles(File[] Files, String[] fileNames) throws IOException {

		ActionContext ac = ActionContext.getContext();
		ServletContext sc = (ServletContext) ac.get(StrutsStatics.SERVLET_CONTEXT);
		String path = sc.getRealPath("/");
		for (int i = 0; i < Files.length; i++) {
			// 编辑增值业务时有可能不选择图片
			if (fileNames[i] == null) {
				continue;
			}
			// 写入文件
			FileOutputStream fos = new FileOutputStream(path + fileNames[i]);
			FileInputStream fis = new FileInputStream(Files[i]);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			if (fis != null) {
				fis.close();
			}
			if (fos != null) {
				fos.close();
			}
		}

	}

	/**
	 * 生成页面
	 */
	public void generatePage(String rootPath, String pagePath, String pageUrl) throws IOException {
		SiteResource resource = siteResourceFactory.createSiteResource(pageUrl);
		String dirPath = rootPath.concat(resource.getPath());
		makeDirs(dirPath);
		String destPath = rootPath.concat(resource.getFullPath());
		writePage(pagePath, destPath);
		//log.info("destPath:"+destPath);
	}

	void makeDirs(String dirPath) {
		File file = new File(dirPath);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	public void writePage(String pagePath, String destPath) throws IOException {
		URL url = new URL(pagePath);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.connect();
		InputStream inputStream = connection.getInputStream();
		BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destPath),
				getCharsetName()));
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, getCharsetName()));
		String line;

		while ((line = bufferedReader.readLine()) != null) {
			if (line.trim().length() > 0) {
				bufferedWriter.write(line);
				bufferedWriter.newLine();
			}
		}
		bufferedWriter.close();
		bufferedReader.close();
	}

	public void uploadFile(File file, String name) throws IOException {
		ActionContext ac = ActionContext.getContext();
		ServletContext sc = (ServletContext) ac.get(StrutsStatics.SERVLET_CONTEXT);
		String path = sc.getRealPath("/");
		// 写入文件

		FileOutputStream fos = new FileOutputStream(path + "/" + name);
		FileInputStream fis = new FileInputStream(file);
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
		}
		if (fis != null) {
			fis.close();
		}
		if (fos != null) {
			fos.close();
		}
	}
}
